﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using RhotCMS.Models;
using System.Threading;
using Utils;
using Infrastructure;
using Core.PageEntities;

namespace RhotCMS.Helpers
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString RouteLinkPages(this HtmlHelper helper, string linkTitle, string group, string name)
        {
            string curLang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
            string html = String.Empty;
                        
           
                if (Convert.ToBoolean(CMSSettingsRepository.GetSingleAppSetting("IsMultiLanguage")))
                {
                    if (group.Equals("Store", StringComparison.InvariantCultureIgnoreCase) && name.Equals("Index", StringComparison.InvariantCultureIgnoreCase))
                    {
                        html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Store", new { lang = curLang, controller = "Store", action = "Index" }).ToString());
                    }
                    else
                    {
                        if (String.IsNullOrWhiteSpace(group) && String.IsNullOrWhiteSpace(name))
                        {
                            html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Root/lang", new { lang = curLang, controller = "Pages", action = "Index", group = group, name = name }).ToString());
                        }
                        else if (String.IsNullOrWhiteSpace(group) && !String.IsNullOrWhiteSpace(name))
                        {
                            html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Root/Pages", new { lang = curLang, controller = "Pages", action = "Index", group = group, name = name }).ToString());
                        }
                        else if (!String.IsNullOrWhiteSpace(group) && !String.IsNullOrWhiteSpace(name))
                        {
                            html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Group/Pages", new { lang = curLang, controller = "Pages", action = "Index", group = group, name = name }).ToString());
                        }
                    }
                }
                else
                {
                    if (group.Equals("Store", StringComparison.InvariantCultureIgnoreCase) && name.Equals("Index", StringComparison.InvariantCultureIgnoreCase))
                    {
                        html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Store", new { controller = "Store", action = "Index" }).ToString());
                    }
                    else
                    {
                        if ((String.IsNullOrWhiteSpace(group) && String.IsNullOrWhiteSpace(name)) || (group.Equals("root", StringComparison.InvariantCultureIgnoreCase) && name.Equals("index", StringComparison.InvariantCultureIgnoreCase)))
                        {
                            html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Root", new { controller = "Pages", action = "Index", group = group, name = name }).ToString());
                        }
                        else if ((String.IsNullOrWhiteSpace(group) && !String.IsNullOrWhiteSpace(name)) || group.Equals("root", StringComparison.InvariantCultureIgnoreCase))
                        {
                            html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Root/Pages", new { controller = "Pages", action = "Index", group = group, name = name }).ToString());
                        }
                        else if (!String.IsNullOrWhiteSpace(group) && !String.IsNullOrWhiteSpace(name))
                        {
                            html = HttpUtility.HtmlDecode(helper.RouteLink(linkTitle, "Group/Pages", new { controller = "Pages", action = "Index", group = group, name = name }).ToString());
                        }
                    }
                }           

            return new MvcHtmlString(html);
        }

        public static string GetUILangName(this HtmlHelper helper)
        {
            return Thread.CurrentThread.CurrentUICulture.Name;
        }

        public static IEnumerable<TopMenuPage> GetTopMenuList(this HtmlHelper helper, string uiLangName)
        {
            using (PageManager pageManager = new PageManager())
            {
                return pageManager.GetSitePagesWidthParentContainers(uiLangName).Where(x=>x.SitePageDefinition.IsActive).OrderBy(x=>x.SitePageDefinition.SortOrder).ToList().Select(x => new TopMenuPage() { Name = x.Title, ActionName = x.SitePageDefinition.InternalName, ControllerName = x.SitePageDefinition.SitePageGroup.GroupName });
            }
        }
    }
}